package cn.tedu.egcar1.controller;

import cn.tedu._03vehicle.pojo.entity.Vehicle;
import cn.tedu.egcar1.base.response.JsonResult;
import cn.tedu.egcar1.base.response.StatusCode;
import cn.tedu.egcar1.mapper.VehicleMapper;
import cn.tedu.egcar1.pojo.dto.VehicleAddParam;
import cn.tedu.egcar1.pojo.dto.VehicleListParam;
import cn.tedu.egcar1.pojo.vo.UserVO;
import cn.tedu.egcar1.pojo.vo.VehicleListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/v1/vehicle/")
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;

    @ApiOperation(value="添加车辆")
    @ApiOperationSupport(order=10)
    @PostMapping("add")
    public JsonResult addVehicle(@RequestBody VehicleAddParam vehicleAddParam, @ApiIgnore HttpSession session){
        log.debug("vehicleAddParam:"+vehicleAddParam);
        UserVO userVO =(UserVO) session.getAttribute("user");
        if (userVO==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam,vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        vehicle.setUserId(userVO.getId());
        vehicleMapper.addVehicle(vehicle);
        return JsonResult.ok();
    }

    @ApiOperation(value="车辆列表")
    @ApiOperationSupport(order=20)
    @GetMapping("list")
    public JsonResult vehicleList(VehicleListParam vehicleListParam, @ApiIgnore HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        String brand = vehicleListParam.getBrand();
        String license = vehicleListParam.getLicense();
        Long userId = userVO.getId();
        VehicleListVO vehicleListVO =vehicleMapper.vehicleList(brand,license,userId);
        return JsonResult.ok(vehicleListVO);
    }

    @ApiOperation(value="删除车辆")
    @ApiOperationSupport(order=30)
    @PostMapping("del/{id}")
    public JsonResult deleteVehicle(@PathVariable Long id,@ApiIgnore HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        int a = vehicleMapper.deleteVehicle(id);
        if (a<0){
            return new JsonResult(StatusCode.OPERATION_FAILED,"该车辆不存在");
        }
        return JsonResult.ok();
    }

}
